package cn.initcap.concurrency.concurrency.atomic;

import cn.initcap.concurrency.annoations.ThreadSafe;
import cn.initcap.concurrency.concurrency.ThreadPoolCreatHelper;
import java.util.concurrent.atomic.LongAdder;
import lombok.extern.slf4j.Slf4j;

/**
 * LongAdder VS AtomicLong
 * 对于普通类型的long double变量，JVM允许将64位的读操作或者写操作拆成两个32位的操作
 * LongAdder通过热点分离，哈希算法大大的提高了运行效率
 * AtomicLong 通过Unsafe类的compareAndSwap方法的循环尝试保证了底层变量和私有拷贝的一致性
 *
 * @author initcap
 * @date Created in 2018/6/13 PM7:41.
 */
@ThreadSafe
@Slf4j
public class LongAdderExample {
    /**
     * 总请求数
     */
    private static final int CLIENT_TOTAL = 5000;
    /**
     * 同时并发执行的线程数
     */
    private static final int THREAD_TOTAL = 200;

    private static LongAdder count = new LongAdder();

    public static void add() {
        count.increment();
    }

    public static void main(String[] args) throws Exception {
        ThreadPoolCreatHelper.threadPoolRun(CLIENT_TOTAL, THREAD_TOTAL,
                LongAdderExample.class, "add",
                "LongAdderExample");
        log.info("count:{}", count);
    }

}
